package com.yupi.yuoj.interceptor;

import cn.hutool.core.lang.UUID;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
@Slf4j
public class TraceIdInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String traceId = String.format("%s - traceId=%s", request.getRequestURI(), UUID.fastUUID().toString(true));
        String remoteHost = request.getRemoteHost();
        log.info("访问ip={}",remoteHost);
        MDC.put("traceId", traceId);
        return true;
    }
}